package cm.iccsoft.android.telecomcost.activities;

import java.util.ArrayList;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.Utility;
import cm.iccsoft.android.telecomcost.entities.TimePeriod;
import cm.iccsoft.android.telecomcost.entities.WeeklyThreshold;
import cm.iccsoft.android.telecomcost.entities.ZoneThreshold;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import cm.iccsoft.android.telecomcost.ui.TimePeriodSpinner;
import cm.iccsoft.android.telecomcost.ui.WeeklyThresholdPickerDialog;

public class ZoneThresholdListActivity extends TelecomcostActivity {

	private ListView threshold_list;
	private TimePeriodSpinner spinnerPeriods;
	private DatabaseManager db;
	private ArrayList<ZoneThreshold> thresholds;

	public ArrayList<ZoneThreshold> getThresholds() {
		return thresholds;
	}

	@Override
	public void setData(Bundle data) {
		super.setData(data);
		
		if(data.containsKey(WeeklyThreshold.PROP_TYPE))
		{
			WeeklyThreshold threshold = TelecomCost.getGson().fromJson(data.getString(WeeklyThreshold.PROP_TYPE),WeeklyThreshold.class);
		}
	}



	public void setThresholds(ArrayList<ZoneThreshold> thresholds) {
		this.thresholds = thresholds;
	}

	public static final int DIALOG_PICK_WEEKLY_THRESHOLD = 1;	

	public Dialog onCreateDialog(int id) {
		Log.i("ZoneThresholdListActivity", "Creating dialog ohne bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_PICK_WEEKLY_THRESHOLD:
			dialog = new WeeklyThresholdPickerDialog(ZoneThresholdListActivity.this);
			break;
		default:
			dialog = null;
		}

		return dialog;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		Log.i("ZoneThresholdListActivity", "Creating dialog with bundle");
		Dialog dialog = null;
		switch (id) {
		case DIALOG_PICK_WEEKLY_THRESHOLD:
			dialog = new WeeklyThresholdPickerDialog(ZoneThresholdListActivity.this);
			break;
		default:
			dialog = null;
		}

		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		Log.i("ZoneThresholdListActivity", "Preparing dialog with bundle");
		super.onPrepareDialog(id, dialog, args);		
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		Log.i("ZoneThresholdListActivity", "Preparing dialog ohne bundle");
		super.onPrepareDialog(id, dialog);
		Utility.setDialogLayout(dialog, LayoutParams.WRAP_CONTENT, 99);
	}

	/*
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("ZoneThresholdListActivity", "Creating the activity");

		super.onCreate(savedInstanceState);
		db = TelecomCost.getDatabaseManager();

		setContentView(R.layout.zone_threshold_list);

		spinnerPeriods = (TimePeriodSpinner) findViewById(R.id.spinnerPeriods);
		threshold_list = (ListView) findViewById(R.id.listViewThreshold);
		
		spinnerPeriods.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {				
				showThresholdDialog(arg0,arg2);				
			}

			public void onNothingSelected(AdapterView<?> arg0) {				
			}
		});
	}

	protected void showThresholdDialog(AdapterView<?> adapter,int index) {
		TimePeriod period = (TimePeriod) adapter.getAdapter().getItem(index);
		Log.i("ZoneThresholdListActivity",period.toString() + " period selected");
		switch(period)
		{
		case DAY:
			showDialog(DIALOG_PICK_WEEKLY_THRESHOLD);
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.i("ZoneThresholdListActivity", "creating options menu for providers : ");
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_thresholds, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.i("ZoneThresholdListActivity", "onOptionsItemSelected");
		switch (item.getItemId()) {
		case R.id.menu_item_edit_thresholds:
			showDialog(DIALOG_PICK_WEEKLY_THRESHOLD);
			return true;		
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i("ZoneThresholdListActivity", "onActivityResult");
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		Log.i("ZoneThresholdListActivity", "Destroying the activity");
		super.onDestroy();		
	}

	@Override
	protected void onPause() {
		Log.i("ZoneThresholdListActivity", "Pausing the activity");
		super.onPause();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		Log.i("ZoneThresholdListActivity", "Postcreating the activity");
		super.onPostCreate(savedInstanceState);
	}

	@Override
	protected void onPostResume() {
		Log.i("ZoneThresholdListActivity", "PostResuming the activity");
		super.onPostResume();
		spinnerPeriods.setAdapter(null);
		bindThresholds();
	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		Log.i("ZoneThresholdListActivity",
				"activity has been attached to the window manager");
	}

	@Override
	protected void onRestart() {
		Log.i("ZoneThresholdListActivity", "Restarting the activity");
		super.onRestart();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.i("ZoneThresholdListActivity", "onRestoreInstanceState the activity");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		Log.i("ZoneThresholdListActivity", "Resuming the activity");
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.i("ZoneThresholdListActivity", "onSaveInstanceState the activity");
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onStart() {
		Log.i("ZoneThresholdListActivity", "Starting the activity");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.i("ZoneThresholdListActivity", "Stopping the activity");
		super.onStop();
	}

	public void bindThresholds() {
//		Log.i("ZoneThresholdListActivity", "binding ZoneThreshold...");
//		thresholds = thresholds == null ? db.getZoneThreshold() : thresholds;
//
//		threshold_list.setAdapter(new ZoneThresholdAdapter(ZoneThresholdListActivity.this,
//				R.layout.threshold_view, thresholds, this));
	}

	@Override
	public String toString() {
		return "ZoneThresholdListActivity";
	}
	
	public void setMonthlyThresgold(ZoneThreshold threshold) throws Exception {
//		if (thresholds != null) {
//			Log.i("WeeklyThresholdListActivity", "updating ZoneThreshold in list activity...");
//			int index = 0;
//			while (index < thresholds.size()) {
//				ZoneThreshold _threshold = thresholds.get(index);
//				if (_threshold.getMonth() == threshold.getMonth()) {
//					_threshold = threshold;					
//					return;
//				}
//
//				index++;
//			}
//		}
//
//		throw new Exception(
//				"could not set the monthly threshold...does not exist in the list");
	}
}
